#!/usr/bin/python
import Tkinter
import main
from Tkconstants import *
import sys
import threading

def start():
    global t_timeout
    global t_port
    global t_proxys_box
    global t_results_box
    global start_button
    global t_good_servers_box

    start_button.configure(state=Tkinter.DISABLED)

    check_process = main.proxy_checker() 
    check_process.set_timeout(int(t_timeout.get()))
    proxys = check_process.read_proxys(t_proxys_box.get('1.0','end'),t_port.get())
    
    good_proxys = []
    for proxy in proxys:
        try:
            t_results_box.insert(Tkinter.END,'\n'+proxy+'\n')
            t_results_box.see(Tkinter.END)
            result = check_process.check_proxy(proxy)
            if result == 1: 
                t_results_box.insert(Tkinter.END,"Successfull\n")
                good_proxys.append(proxy)
                t_good_servers_box.insert(Tkinter.END,proxy+'\n')
                t_good_servers_box.see(Tkinter.END)
            if result == 0: t_results_box.insert(Tkinter.END,"Cant connect to Google\n")
            if result == 2: t_results_box.insert(Tkinter.END,"Cant connect to proxyserver\n")
            if result == 3: raise KeyboardInterrupt
        except KeyboardInterrupt:
            sys.exit(0) 
        except: pass
        t_results_box.see(Tkinter.END)
    
    start_button.configure(state=Tkinter.ACTIVE)

tk = Tkinter.Tk()
tk.title("Flo's Proxy Validator")

gui_interface = Tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
gui_interface.pack(fill=BOTH,expand=1)

l_timeout = Tkinter.Label(gui_interface, text="Timeout")
l_timeout.grid(column=0, row=0, columnspan=3)

t_timeout = Tkinter.StringVar()
t_timeout_box = Tkinter.Entry(gui_interface,textvariable=t_timeout,width=10,justify="center")
t_timeout_box.grid(column=0,row=1,columnspan=3)
t_timeout_box.insert(0,"5")

l_port = Tkinter.Label(gui_interface, text="Port")
l_port.grid(column=0, row=0, columnspan=1)

t_port = Tkinter.StringVar()
t_port = Tkinter.Entry(gui_interface,textvariable=t_port,width=10,justify="center")
t_port.grid(column=0,row=1,columnspan=1)
t_port.insert(0,"80")

l_proxys = Tkinter.Label(gui_interface, text="Proxy Server:")
l_proxys.grid(column=0,row=2)

t_proxys_box = Tkinter.Text(gui_interface,height=10,width=30)
t_proxys_box.grid(column=0,row=3)
scrollbar_input = Tkinter.Scrollbar(gui_interface,command=t_proxys_box.yview)
t_proxys_box.config(yscrollcommand=scrollbar_input.set)
scrollbar_input.grid(column=1,row=3,sticky=Tkinter.N+Tkinter.S)

l_results = Tkinter.Label(gui_interface, text="Results:")
l_results.grid(column=0,row=4)

t_results_box = Tkinter.Text(gui_interface,height=10,width=30)
t_results_box.grid(column=0,row=5)
scrollbar_output = Tkinter.Scrollbar(gui_interface,command=t_results_box.yview)
t_results_box.config(yscrollcommand=scrollbar_output.set)
scrollbar_output.grid(column=1,row=5,sticky=Tkinter.N+Tkinter.S)

l_good_servers = Tkinter.Label(gui_interface, text="Good Servers:")
l_good_servers.grid(column=2,row=2)

t_good_servers_box = Tkinter.Text(gui_interface,height=22,width=25)
t_good_servers_box.grid(column=2,row=3,rowspan=3)
scrollbar_good_servers = Tkinter.Scrollbar(gui_interface,command=t_good_servers_box.yview)
t_good_servers_box.config(yscrollcommand=scrollbar_good_servers.set)
scrollbar_good_servers.grid(column=3,row=3,rowspan=3,sticky=Tkinter.N+Tkinter.S)

def start_thread(): 
    t_results_box.delete(1.0, Tkinter.END)
    threading.Thread(None,start).start()

start_button = Tkinter.Button(gui_interface, text="start",command=start_thread,width=25)
start_button.grid(column=0,row=6,columnspan=3)

tk.mainloop()
